﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Keepo.Helpers;

namespace AlbumDesigner.Helpers
{
    public class EncodeHelper
    {
        public static Byte[] EncodeBitmap(Bitmap img, string contentType)
        {
            EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)96 /*quality*/);
            ImageCodecInfo imageCodec = ImageHelper.GetEncoderInfo(contentType);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            MemoryStream ms = new MemoryStream();
            img.Save(ms, imageCodec, encoderParams);

            // make byte array the same size as the image
            byte[] imageContent = new Byte[ms.Length];

            // rewind the memory stream
            ms.Position = 0;

            // load the byte array with the image
            ms.Read(imageContent, 0, (int)ms.Length);

            return imageContent;
        }

    }
}